package com.lmy.springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

@Controller
@Slf4j
public class UploadController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam(value = "name", required = false) String name,
                         @RequestParam(value = "email", required = false) String email,
                         @RequestParam(value = "age", required = false) Integer age,
                         @RequestParam(value = "job", required = false) String job,
                         @RequestParam(value = "header", required = false)MultipartFile file,
                         @RequestParam(value = "photos", required = false)MultipartFile[] photos) throws IOException {
        log.info("输出获取到的信息:name={},email={},age={},job={},header={},photos={}",
                name, email, age, job, file, photos);
        //将文件保存到指定目录，如D盘下temp_upload
//        if (!file.isEmpty()) {  //处理header文件
//            //获取上传文件的名称
//            String originalFilename = file.getOriginalFilename();
//            file.transferTo(new File("d:\\temp_upload\\" + originalFilename));
//        }
//        if (photos.length > 0) {    //处理photos文件
//            for (MultipartFile photo : photos) {    //多个文件，遍历处理
//                if (!photo.isEmpty()) {
//                    String originalFilename = photo.getOriginalFilename();
//                    photo.transferTo(new File("d:\\temp_upload\\" + originalFilename));
//                }
//            }
//        }

        //动态保存文件 如类路径下 /static/images/upload(文件覆盖问题、分目录存放问题)
        //1.动态获取类路径
        String classPath = ResourceUtils.getURL("classpath:").getPath();
        log.info("classPath={}", classPath); // /D:/IdeaCode/springboot/thymeleaf/target/classes/
        //2.自定义要上传的目录，如果不存在就先创建
        //完成按年月日动态创建目录完成分目录上传
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        String filePath = "static/image/upload/" + year + "/" + month + "/" + day + "/";
        File f = new File(classPath + filePath);
        if (!f.exists()) {
            f.mkdirs();
        }
        //根据全路径来保存文件
        //文件名拼接时间戳解决文件替换问题
        long current = System.currentTimeMillis();
        if (!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String s = String.valueOf(current);
            String fileName = originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "_" + s + (originalFilename.substring(originalFilename.lastIndexOf(".")));
            file.transferTo(new File(f.getAbsolutePath() + "/" + fileName));
        }
        if (photos.length > 0) {    //处理photos文件
            for (MultipartFile photo : photos) {    //多个文件，遍历处理
                if (!photo.isEmpty()) {
                    String originalFilename = photo.getOriginalFilename();
                    String s = String.valueOf(current);
                    String fileName = originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "_" + s + (originalFilename.substring(originalFilename.lastIndexOf(".")));
                    photo.transferTo(new File(f.getAbsolutePath() + "/" + fileName));
                }
            }
        }
        return "注册用户成功/文件上传成功";
    }
}
